import { Model, DataTypes } from 'sequelize'
import { sequelize } from '../config/index.js'
import User from './User.js'

// 定义一个 Post 类 继承 Model
class Post extends Model {}

// 通过 init 方法定义表的字段等信息
Post.init(
  {
    title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    views: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
  },
  { sequelize, modelName: 'Post' }
)

//设置 User 表与 Post 表之间的关系 (一对多)
Post.belongsTo(User, { foreignKey: 'userID' })
User.hasMany(Post, { foreignKey: 'userID' })

export default Post